package com.srit.forecast.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.srit.core.bean.Page;
import com.srit.core.sql.BaseResultSetExtractor;
import com.srit.forecast.bean.ReportPageRel;
import com.srit.forecast.dao.ReportPageRelDao;

@Service
@Transactional
public class ReportPageRelDaoImpl implements ReportPageRelDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public void save(ReportPageRel reportPageRel) {
		String sql = "insert into TC_REPORTPAGEREL(ID，NAME，REPORTCONFIGID,PAGECONFIGID)values(SEQ_TC_REPORTPAGEREL.nextval,?,?,?)";
		jdbcTemplate.update(
				sql,
				new Object[] { reportPageRel.getName(),
						reportPageRel.getReportConfigId(),
						reportPageRel.getPageConfigId() });
	}

	@Override
	public void update(ReportPageRel reportPageRel) {
		String sql = "update  TC_REPORTPAGEREL set NAME=?，REPORTCONFIGID=?,PAGECONFIGID=? where id=? ";
		jdbcTemplate
				.update(sql,
						new Object[] { reportPageRel.getName(),
								reportPageRel.getReportConfigId(),
								reportPageRel.getPageConfigId(),
								reportPageRel.getId() });

	}

	@Override
	public ReportPageRel getById(Integer id) {
		String sql = "select * from TC_REPORTPAGEREL where id=?";
		return jdbcTemplate.queryForObject(sql, new Object[] { id },
				new ReportPageRelRowMapper());
	}

	@Override
	public void delete(Integer id) {
		String sql = "DELETE from TC_REPORTPAGERELwhere id=?  ";
		jdbcTemplate.update(sql, new Object[] { id });
	}

	@Override
	public void page(Page page) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getMaxId() {
		String sql = "select max(ID) from TC_REPORTPAGEREL";
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public void deletByReportConfigId(int reportConfigId) {
		String sql = "delete from TC_REPORTPAGEREL where REPORTCONFIGID=?";
		jdbcTemplate.update(sql, reportConfigId);
	}

	@Override
	public List<ReportPageRel> listByReportConfigId(int reportConfigId) {
		String sql = "select * from TC_REPORTPAGEREL where REPORTCONFIGID=?";
		List<ReportPageRel> result = jdbcTemplate.query(sql,
				new Object[] { reportConfigId }, new ReportPageRelRowMapper());
		return result;
	}

}

class ReportPageRelResultSetExtractor extends
		BaseResultSetExtractor<ReportPageRel> {
}

class ReportPageRelRowMapper implements RowMapper<ReportPageRel> {

	@Override
	public ReportPageRel mapRow(ResultSet rs, int rowNum) throws SQLException {
		ReportPageRelResultSetExtractor extractor = new ReportPageRelResultSetExtractor();
		return extractor.extractData(rs);
	}

}
